import cv2
import numpy as np

'''
A:
r    = (pix[x,y]-avg(R))
dst[x,y] = 128+|r|*r/(2*R)
B:
r=(pix[x,y]-avg(R))/128
pix[x,y]*r+128*(1-r)
'''

img = cv2.imread("face1.jpg", cv2.IMREAD_UNCHANGED)
# cv2.imshow('original' ,img)
# dst = cv2.bilateralFilter(img,19,85,75)
# cv2.imshow('bilateral' ,dst)
k1Size = 9
k2Size = 3
gaussian = cv2.GaussianBlur(img, ksize=(k1Size, k1Size), sigmaX=0, sigmaY=0)
# cv2.imshow('Graussian', highpass)
# r = img - cv2.bilateralFilter(img,k2Size,0,40)
r = img - cv2.blur(img, (k2Size, k2Size))
# r = img - cv2.GaussianBlur(img, ksize=(k2Size, k2Size), sigmaX=0, sigmaY=0)
highpass = np.uint8(np.clip(128 + np.abs(r)*r/(2*k2Size), 0, 255))
# cv2.imshow('high pass', highpass)

# 线性光叠加
# 基色 + 2混合色 - 255 = 结果色。
softskin = np.clip((gaussian + highpass - 127), 0,255)

img1 = np.hstack((img, gaussian))
img2 = np.hstack((highpass, softskin))
img = np.vstack((img1, img2))
cv2.imshow('soft skin', img)


cv2.waitKey(0)
cv2.destroyAllWindows()